import Vue from 'vue'
import Vuex from 'vuex'
import request from 'common/request.js'
Vue.use(Vuex) // vue的插件机制

// Vuex.Store 构造器选项
const store = new Vuex.Store({
	// 为了不和页面或组件的data中的造成混淆，state中的变量前面建议加上$符号
	state: {
		//当前锁定的时间
		yearMonth:{
			year:"",
			month:"",
		},
		// 用户信息
		userInfo: null,
	/*	userInfo: {
			"encryptedData": "3gdH8KOs2tnmN88d13f1/2+o9tlhHc+WxwjM8civgCxDshKsgtMOlPetdU0O+e07ZyIMxaXDQqufAhDNIOWvfo32ciAKKQr4NBPUl5r7Qk5RnElQDc+4SClEyx7fKk5OCouAvL9Ztqrl9JbaD5hIk36IeIAoakaD2zyXi6FWy8fSwgH7VN3HvyBXctkANTsYWUHdpsgGSvEY/UblpdNZT8+UnuPWrNW4IMNjWPzuWCvx7Wsj9NDyZWOdAiuULMM70jl1Lqkn0yvuXH7LYntzqO2nJuw9Esyj0p0SECOjZRLZM70VAEXlAHsNM23oojm3z/ClCTwfmdUJ9rASrLaXcV67JSosSrfen8S8qPAU+ykLDG6ATUryFk6ks09Jtoze16M9d1GEnmpuk92quTG9Mhz96QY48W1e7SSphsZ4zWA=",
			"iv": "ZlwpbUwCZFonONjATYx9Qw==",
			"signature": "3766b59adefed626b010f9cbeed43ea14561c931",
			"userInfo": {
				"nickName": "佳伦不是桂伦",
				"gender": 0,
				"language": "zh_CN",
				"city": "",
				"province": "",
				"country": "",
				"avatarUrl": "https://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83epzf6xib6HyMianvmjMWqoUord15ju2uEUsLuVYtgVk1ickf438WttDsNPb7SG95u4weUGhpQgwAhRGA/132"
			},
			"rawData": "{\"nickName\":\"佳伦不是桂伦\",\"gender\":0,\"language\":\"zh_CN\",\"city\":\"\",\"province\":\"\",\"country\":\"\",\"avatarUrl\":\"https://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83epzf6xib6HyMianvmjMWqoUord15ju2uEUsLuVYtgVk1ickf438WttDsNPb7SG95u4weUGhpQgwAhRGA/132\"}",
			"errMsg": "getUserProfile:ok"
		},*/
	},
	mutations: {
		login(state, fun) {
			var data = {}
			//获取用户信息
			wx.getUserProfile({
				desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
				success: info => {
					data["nickName"] = info.userInfo.nickName;
					data["rawData"] = info.rawData;
					data["signature"] = info.signature;
					data["encryptedData"] = info.encryptedData;
					data["iv"] = info.iv;
					wx.login({
						success: res => {
							console.log("获取code:"+res.code)
							data["code"] = res.code;
							if (res.code) {
								//发起网络请求
								request("/sys-login/wxLogin",JSON.stringify(data),"post").then(res=>{
									uni.setStorageSync("token",res.token);
									state.userInfo = info;
									uni.$u.toast("登录成功")
									if(fun!=null && fun!=""){
										fun();
									}
								}).catch(err=>{
									uni.$u.toast(""+err.msg)
									//登录失败清楚token
									uni.removeStorageSync('token');
									uni.$u.toast(err.msg)
									//登录成功后回调
								})
							} else {
								uni.$u.toast('登录失败！' + res.errMsg)
							}

						}
					})

				}
			})
		},

		//设置当前年月
		setYearMonth(state, yearMonth) {
			state.yearMonth=yearMonth;
		}
	},
	getters: {
		getUserInfo(state) {
			return state.userInfo;
		},

		//获取当前年月
		getYearMonth(state) {
			return state.yearMonth;
		}
	},

})

export default store
